//
//  KZUnfamiliarVocabularyImproveVC.m
//  听说弱点强化训练
//
//  Created by 朱凯 on 2018/9/6.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "KZUnfamiliarVocabularyImproveVC.h"

// vc
#import "KZVocabularyImproveMakeTestVC.h"
#import "KZUserCenterVC.h"
#import "LSWrongQuesController.h"

// view
#import "KZRandomDistributionView.h"        // 随机分布视图
#import "KZShadowButton.h"                  // 阴影按钮
#import "KZRandomSphereView.h"

// api
#import "KZVocabularyImproveNetApi.h"
#import "ZBFtpTools.h"
#import "LSWrongQuesListManager.h"

@interface KZUnfamiliarVocabularyImproveVC ()

// 错题的
@property (nonatomic, strong) UILabel *wrongLabel;
@property (nonatomic, strong) KZShadowButton *beginWrongButton;

// 生词的
//@property (nonatomic, strong) KZRandomDistributionView *disView;
@property (nonatomic, strong) KZRandomSphereView *disView;

@property (nonatomic, strong) UILabel *masterLabel;
@property (nonatomic, strong) KZShadowButton *beginButton;
@property (nonatomic, strong) KZVocabularyPackageModel *packageModel;

@property (nonatomic, strong) LSModelWrongQuesList *model;



@end

@implementation KZUnfamiliarVocabularyImproveVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
        
    [self setupNaviUI];
    [self setupUI];
    
    
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    // 获取词汇包信息
    [self getUnfamiliarVocabularyPackage];
    //获取错题信息
    [self getWrongQuesListInfo];
}
- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    [_disView removeAnimation];
}

#pragma mark - 创建视图
- (void)setupNaviUI {
    
//    self.title = @"错题生词强化训练";
//    [self addLeftBackBarButtonItem];
//
//    UIButton *userButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    userButton.frame = CGRectMake(0, 0, 39, 44);
//    [userButton setImageEdgeInsets:UIEdgeInsetsMake(11, 10, 11, 10)];
//    [userButton setImage:[UIImage imageNamed:@"头像"] forState:UIControlStateNormal];
//    [userButton addTarget:self action:@selector(enterUserCenter) forControlEvents:UIControlEventTouchUpInside];
//
//    [self addCustomItemviews:@[userButton] position:ByNavigationItemPositionModeRight];
    
    NSString *text = @"错题生词强化训练";
    
    UIControl *backView = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    [backView addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backView];
    
    UIImageView *backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    backImageView.image = [UIImage imageNamed:@"kz-关闭"];
    [backView addSubview:backImageView];
    [backImageView sizeToFit];
    backImageView.centerY = backView.height / 2;
    
    UIImageView *iconView = [[UIImageView alloc] initWithFrame:CGRectMake(backImageView.right+5, 10, 20, 20)];
    iconView.image = [UIImage imageNamed:@"kz-左上角错题生词图标"];
    [backView addSubview:iconView];
    [iconView sizeToFit];
    iconView.centerY = backView.height / 2;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(iconView.right+5, 0, [text getSizeOfBlodString:20].width, 40)];
    label.adjustsFontSizeToFitWidth = true;
    label.numberOfLines = 1;
    label.text = text;
    label.font = B_FONT(20);
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    [backView addSubview:label];
    
    backView.width = label.right;
    
}

- (void)setupUI {
    
    // 顶部注释
    UILabel *topNoteLabel = [[UILabel alloc] init];
    topNoteLabel.adjustsFontSizeToFitWidth = true;
    topNoteLabel.text = @"针对错题和生词采用“错题重做+生词学习”的配合训练，提升听说能力！";
    topNoteLabel.font = FONT(11);
    topNoteLabel.textAlignment = NSTextAlignmentCenter;
    topNoteLabel.textColor = [UIColor grayColor];
    [self.view addSubview:topNoteLabel];
    [topNoteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(@0);
        make.height.mas_equalTo(H(35));
    }];
    
    
    
    // 错题
    UIView *wrongView = [UIView new];
    wrongView.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:wrongView];
    [wrongView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topNoteLabel.mas_bottom);
        make.left.right.equalTo(@0);
        // 高度内部决定
    }];
    
    _wrongLabel = [[UILabel alloc] init];
    _wrongLabel.adjustsFontSizeToFitWidth = true;
    _wrongLabel.text = @"当前总计错题数量：0";
    _wrongLabel.font = FONT(16);
    _wrongLabel.textAlignment = NSTextAlignmentCenter;
    _wrongLabel.textColor = [UIColor blackColor];
    [wrongView addSubview:_wrongLabel];
    [_wrongLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.top.mas_equalTo(H(20));
    }];
    
    UILabel *wrongNoteLabel = [[UILabel alloc] init];
    wrongNoteLabel.adjustsFontSizeToFitWidth = true;
    wrongNoteLabel.numberOfLines = 2;
    wrongNoteLabel.text = @"建议你先进行错题重做训练，系统将按“听力选择题 > 跟读题 > 朗读题”的顺序，再结合更新时间的顺序进行出题。";
    wrongNoteLabel.font = FONT(12);
    wrongNoteLabel.textAlignment = NSTextAlignmentLeft;
    wrongNoteLabel.textColor = [UIColor grayColor];
    [wrongView addSubview:wrongNoteLabel];
    [wrongNoteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(W(15));
        make.right.mas_equalTo(W(-15));
        make.top.equalTo(_wrongLabel.mas_bottom).offset(H(13));
    }];
    
    KZShadowButton *beginWrongButton = [[KZShadowButton alloc] init:^(UIButton *button) {
        button.backgroundColor = KZThemeColor2;
        [button setTitle:@"进入错题重做" forState:UIControlStateNormal];
    }];
    _beginWrongButton = beginWrongButton;
    [beginWrongButton addTarget:self action:@selector(beginWrong) forControlEvents:UIControlEventTouchUpInside];
    [wrongView addSubview:beginWrongButton];
    [beginWrongButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(wrongView);
        make.width.equalTo(wrongView).multipliedBy(0.5);
        make.height.mas_equalTo(H(40));
        make.top.equalTo(wrongNoteLabel.mas_bottom).offset(H(20));
        make.bottom.mas_equalTo(-H(20));
    }];
    
    
    // 生词
    UIView *unfamiliarView = [UIView new];
    unfamiliarView.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:unfamiliarView];
    [unfamiliarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(wrongView.mas_bottom).offset(H(5));
        make.left.right.equalTo(@0);
        if (@available(ios 11.0,*)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.bottom.equalTo(@0);
        }
    }];
    
    // 按钮
    _beginButton = [[KZShadowButton alloc] init:^(UIButton *button) {
        button.backgroundColor = KZThemeColor;
        [button setTitle:@"进入生词听说训练" forState:UIControlStateNormal];
    }];
    [_beginButton addTarget:self action:@selector(beginUnfamiliar) forControlEvents:UIControlEventTouchUpInside];
    [unfamiliarView addSubview:_beginButton];
    [_beginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.equalTo(self.view).multipliedBy(0.5);
        make.height.mas_equalTo(H(40));
        make.bottom.mas_equalTo(-H(20));
    }];
    
    // 标注
    _masterLabel = [[UILabel alloc] init];
    _masterLabel.font = FONT(12);
    _masterLabel.textAlignment = NSTextAlignmentCenter;
    _masterLabel.textColor = [UIColor grayColor];
    [unfamiliarView addSubview:_masterLabel];
    [_masterLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.bottom.equalTo(_beginButton.mas_top).offset(H(-20));
    }];
    // 标注
    UILabel *note2Label = [[UILabel alloc] init];
    note2Label.tag = 222;
    note2Label.text = @"当前词汇掌握分析";
    note2Label.font = FONT(16);
    note2Label.textAlignment = NSTextAlignmentCenter;
    note2Label.textColor = [UIColor blackColor];
    [unfamiliarView addSubview:note2Label];
    [note2Label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_masterLabel);
        make.bottom.equalTo(_masterLabel.mas_top).offset(H(-5));
    }];
    
    _disView = [KZRandomSphereView new];
    [unfamiliarView addSubview:_disView];
    [_disView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(@0);
        make.width.equalTo(unfamiliarView).multipliedBy(0.9);
        make.top.equalTo(@0);
        make.bottom.equalTo(note2Label.mas_top).offset(H(-20));
    }];
    
    
}

#pragma mark - Net
// 获取听说词汇包信息
- (void)getUnfamiliarVocabularyPackage {
    
    [MBProgressHUD showLoadind:@"正在查询待下一生词包，请稍候..."];
    
    KZVocabularyImproveNetApi *api = [KZVocabularyImproveNetApi new];
    api.requestParam = @{@"stageNo":KGetUserDefaults(@"TestLayer"),
                         @"schoolId":DR_UserInfo.SchoolID,
                         @"stuId":DR_UserInfo.UserID,};
    [api getUnfamiliarVocabularyPackage:^(KZVocabularyPackageModel *model) {
        
        [MBProgressHUD hideHud];
        
        _packageModel = model;
        
        if (_packageModel.currentPackage.Detail.count >= 20) {
            _disView.vocabularys = [_packageModel.currentPackage.Detail subarrayWithRange:NSMakeRange(0, 20)];
        } else {
            _disView.vocabularys = _packageModel.currentPackage.Detail;
        }
        
        // 同时为0，按钮不能点，且隐藏masterlabel
        if (model.PoorMasterVocabularyNum.intValue == 0 &&
            model.BetterMasterVocabularyNum.intValue == 0) {
            _beginButton.button.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
            _beginButton.enabled = false;
            _masterLabel.hidden = true;
        } else {
            if (model.PoorMasterVocabularyNum.intValue == 0) {
                _beginButton.button.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
                _beginButton.enabled = false;
            } else {
                _beginButton.button.backgroundColor = KZThemeColor;
                _beginButton.enabled = true;
            }
            
            _masterLabel.hidden = false;
            
            NSString *text = [NSString stringWithFormat:@"掌握较好%@个，较弱%@个", model.BetterMasterVocabularyNum, model.PoorMasterVocabularyNum];
            NSString *better = [NSString stringWithFormat:@"%@", model.BetterMasterVocabularyNum];
            NSString *poor = [NSString stringWithFormat:@"%@", model.PoorMasterVocabularyNum];
            NSMutableAttributedString *attrText = [[NSMutableAttributedString alloc] initWithString:text];
            [attrText addAttribute:NSForegroundColorAttributeName value:UIColor.redColor range:[text rangeOfString:better]];
            [attrText addAttribute:NSFontAttributeName value:FONT(H(15)) range:[text rangeOfString:better]];
            [attrText addAttribute:NSForegroundColorAttributeName value:UIColor.redColor range:[text rangeOfString:poor]];
            [attrText addAttribute:NSFontAttributeName value:FONT(H(15)) range:[text rangeOfString:poor]];
            _masterLabel.attributedText = attrText.copy;
        }
        
        [self.view viewWithTag:222].hidden = _masterLabel.hidden;
        [_beginButton setNeedsLayout];
        [_beginButton layoutIfNeeded];
        
    } failuer:^(NSString *error) {
        [MBProgressHUD showFailureWithText:error];
    }];
}

- (void)getWrongQuesListInfo {
    LSWrongQuesListManager *manager = [[LSWrongQuesListManager alloc] init];
    NSDictionary *dict = @{@"StuID":DR_UserInfo.UserID,
                           @"SchoolID":KGetUserDefaults(@"SchoolID"),
                           @"StageNo":KGetUserDefaults(@"TestLayer"),
                           @"TypeID":@"-1"
                           };
    manager.requestParam = @{@"DictString":[NSObject objectToJson:dict]};
    WeakObj(self);
    [manager requestWithCompltionSuccess:^(LSModelWrongQuesList *model) {
        
        selfWeak.model = model;
        if (model.ItemInfoList.count!=0) {
            NSString *ftpServerAddress = _model.FtpServerAddress;
            NSString *ftpUserPwd = _model.sUserPwd;
            NSString *ftpUser = _model.sUserName;
            [ZBFtpTools ZBFtp_setupInitWithHost:ftpServerAddress
                                           user:ftpUser
                                            psw:ftpUserPwd];
            selfWeak.beginWrongButton.button.backgroundColor = KZThemeColor2;
        } else {
            selfWeak.beginWrongButton.button.backgroundColor = [UIColor colorWithHex:0xe1e2e2];
            selfWeak.beginWrongButton.enabled = false;
        }
        
        NSString *text = [NSString stringWithFormat:@"当前总计错题数量:%ld",model.ItemInfoList.count];
        NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:text attributes:@{NSForegroundColorAttributeName:[UIColor redColor]}];
        NSRange range = [text rangeOfString:@"当前总计错题数量:"];
        [attr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:range];
        selfWeak.wrongLabel.attributedText = attr;
        
        
    } failuer:^(NSString *error) {
        
    }];
}

#pragma mark - 事件
- (void)back {
    
    [self dismissViewControllerAnimated:true completion:nil];
}
// 进入个人中心
- (void)enterUserCenter {
    
    KZUserCenterVC *vc = [KZUserCenterVC new];
    [self push:vc];
}
// 错题重做
- (void)beginWrong {
    LSWrongQuesController *vc = [[LSWrongQuesController alloc] init];
    vc.model = self.model;
    [self push:vc];
}
// 生词训练
- (void)beginUnfamiliar {
    
    if (!_packageModel) {
        return;
    }
    if (_packageModel.currentPackage.Detail.count == 0) {
        [MBProgressHUD showFailureWithText:@"没有需要训练的生词了！"];
        return;
    }
    
    KZVocabularyImproveMakeTestVC *vc = [KZVocabularyImproveMakeTestVC new];
    vc.type = KZVocabularyTypeUnfamiliar;
    vc.packageModel = _packageModel;
    [self push:vc];
}




@end
